16. Delete All Pets Menu Option
Now I’d like you to add the code to delete all pets from the database when the user presses the Delete all Pets option from the overflow menu on the CatalogActivity. We’re not adding a confirmation, but you are welcome to you in your own implementation.
When you're finished, the menu should look like this:
This is a relatively small change, so I’ll let you get to it. Complete the following quiz questions to help you complete these code changes.
Resources
Options menu in Android
SOLUTION:
R.id.action_delete_all_entriesSOLUTION:
PetEntry.CONTENT_URIQuiz Solution Explanation
For this quiz, we worked exclusively in the CatalogActivity.java file.
This menu button is associated with R.id.action_delete_all_entries, so under that case in the onOptionsMenuSelected method, I’ll add a call to a method called “deleteAllPets()”.
case R.id.action_delete_all_entries:
deleteAllPets();
return true;
Then in that method, I’ll use the ContentResolver’s delete method and pass the PetEntry.CONTENT_URI. Why the content URI? Because that’s the generic __/pets uri which in our content provider will delete all pets.
/**
* Helper method to delete all pets in the database.
*/
private void deleteAllPets() {
int rowsDeleted = getContentResolver().delete(PetEntry.CONTENT_URI, null, null);
Log.v("CatalogActivity", rowsDeleted + " rows deleted from pet database");
}
Once we've done both of those things, we can test that it works!